/**
 *
 */
(function () {
    // 登录背景变换
    $.backstretch(["module/framework/login/login.jpg", "module/framework/login/login02.jpeg", "module/framework/login/login03.jpg"],
        {
            duration: 3000,
            fade: 750
        });
    // 光标激活背景
    $("input").bind("focus", function () {
        $(".top-content").css({
            opacity: 1
        })
    });
    // 注册回车事件
    document.onkeydown = function (e) {
        var event = document.all ? window.event : e;
        if (event.keyCode === 13) {// 响应回车
            // if (vm.enableLogin) {
            vm.login();
            // }
        }
    };
    var loginTxt = "登  录";
    var loginModel = function () {
        var self = this;
        self._username = ko.observable();
        self._password = ko.observable();
        /**
         * 方法定义  ioioiuououuou
         */
        // self.init = function () {
        //     $('#form-username').text('');
        // };
        /**
         * 登录方法
         */
        self.enableLogin = ko.observable(true);
        self.loginBtnTxt = ko.observable(loginTxt);

        self.login = function () {
            // 清空cookie
            $.cookie('_curUser', '', {
                expires: -1
            });

            if (this._username() === null || undefined === this._username() || this._username() === '') {
                platform.showWarnTip("用户名不能为空!");
                return;
            }
            if (null === this._password() || undefined === this._password() || '' === this._password()) {
                platform.showWarnTip("密码不能为空!");
                return;
            }

            var params = {};
            params.user_name = this._username();
            params.user_pwd = CryptoJS.enc.Base64.stringify(CryptoJS.MD5(this._password()));

            platform.servicebreakAsync("~/rest/shiroLogin", params, 'post',
                function () {
                    platform.showOperTip('VALIDATING');
                    // self.loginBtnTxt('<i class="fa fa-spinner fa-spin"></i>&nbsp;&nbsp;' + loginTxt);
                    // self.enableLogin(false);
                }).done(function (response) {
                if (response instanceof Object) {
                    if (response.meta.success) {

                        self.enableLogin(true);
                        self.loginBtnTxt(loginTxt);

                        // self._username('');
                        // self._password('');// 避免无密码登录
                        if (!response.responseObject.seprateMode) {
                            window.location.href = response.responseObject.redirectUri;
                        }
                        $.cookie('tokenId', response.responseObject.tokenId);
                        // self._password('');// 避免无密码登录
                    } else {
                        platform.showErrorTip(response.meta.message);
                    }
                }
            }).fail(function (response) {
                // console.log(response);
            });
        };
    };
    var vm = new loginModel();
    ko.applyBindings(vm);

    window.onload = function () {
        $('#loginForm').resetForm();
    }
})();